package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TcCheckinRecord;
import com.zhentao.pojo.TcNotice;
import com.zhentao.service.TcCheckinRecordService;
import com.zhentao.service.TcNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/notice")
public class NoticeController {

    @Autowired
    TcNoticeService noticeService;

    @Autowired
    TcCheckinRecordService checkinRecordService;

    /**
     * 获取全部通知
     */
    @GetMapping("list")
    public List<TcNotice> list(){
        return noticeService.list();
    }

    /**
     * 根据ID获取通知详情
     */
    @GetMapping("get/{id}")
    public TcNotice getById(@PathVariable Integer id){
        return noticeService.getById(id);
    }

    /**
     * 添加通知
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody TcNotice notice){
        Map<String, Object> result = new HashMap<>();
        try {
            notice.setCreateTime(new Date());
            boolean success = noticeService.save(notice);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");
            if (success) {
                result.put("data", notice);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新通知
     */
    @PutMapping("update")
    public Map<String, Object> update(@RequestBody TcNotice notice){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = noticeService.updateById(notice);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
            if (success) {
                result.put("data", notice);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除通知
     */
    @DeleteMapping("delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = noticeService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据发布者ID获取通知
     */
    @GetMapping("myNotices/{publisherUserId}")
    public List<TcNotice> getMyNotices(@PathVariable String publisherUserId){
        QueryWrapper<TcNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher_user_id", publisherUserId);
        queryWrapper.orderByDesc("create_time");
        return noticeService.list(queryWrapper);
    }

    /**
     * 根据班级ID获取通知
     */
    @GetMapping("classNotices/{classId}")
    public List<TcNotice> getClassNotices(@PathVariable String classId){
        QueryWrapper<TcNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId);
        queryWrapper.orderByDesc("is_top").orderByDesc("create_time");
        return noticeService.list(queryWrapper);
    }

    /**
     * 获取置顶通知
     */
    @GetMapping("topNotices")
    public List<TcNotice> getTopNotices(){
        QueryWrapper<TcNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_top", 1);
        queryWrapper.orderByDesc("create_time");
        return noticeService.list(queryWrapper);
    }

    /**
     * 搜索通知
     */
    @GetMapping("search")
    public List<TcNotice> searchNotices(@RequestParam String keyword){
        QueryWrapper<TcNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword).or().like("content", keyword);
        queryWrapper.orderByDesc("create_time");
        return noticeService.list(queryWrapper);
    }

    /**
     * 批量删除通知
     */
    @DeleteMapping("batchDelete")
    public Map<String, Object> batchDelete(@RequestBody List<Integer> ids){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = noticeService.removeByIds(ids);
            result.put("success", success);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 设置置顶
     */
    @PutMapping("setTop/{id}")
    public Map<String, Object> setTop(@PathVariable Integer id, @RequestParam Integer isTop){
        Map<String, Object> result = new HashMap<>();
        try {
            TcNotice notice = noticeService.getById(id);
            if (notice != null) {
                notice.setIsTop(isTop);
                boolean success = noticeService.updateById(notice);
                result.put("success", success);
                result.put("message", success ? "设置成功" : "设置失败");
            } else {
                result.put("success", false);
                result.put("message", "通知不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "设置失败：" + e.getMessage());
        }
        return result;
    }

    //获取打卡明细
    @RequestMapping("checkInRecordList")
    public List<TcCheckinRecord> list1(){
        return checkinRecordService.list();
    }
}
